<template>
  <div class="container">
    <div class="containerEart">
      <p class="dialogTitle">重点场所风险评估</p>
      <div class="header">
        <span class="title">震情趋势：</span>
        <div class="icon">
              <svg-icon class-name="error-icon" icon-class="error" style="margin-right:10px" v-if="showIcon"/><span>{{titleOption}}</span>
          </div>
      </div>
    </div>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" @click="openUpload">
          <svg-icon class-name="upload-icon" icon-class="upload" /><span style="margin-left:10px">上传</span>
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" @click="handleDownload">
          <svg-icon class-name="download-icon" icon-class="download" /><span style="margin-left:10px">下载</span>
        </el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="primary" @click="handleSubmit">
          <svg-icon class-name="submit-icon" icon-class="submit" /><span style="margin-left:10px">提交</span>
        </el-button>
      </el-col> -->
    </el-row>

    <el-table v-loading="loading" :data="dataList" stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="评估内容" align="center" prop="krContent" />
      <el-table-column label="生成时间" align="center" prop="krDate" />
      <el-table-column label="业务人员" align="center" prop="krPeople" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button type="text" @click="handleDetil(scope.row)">查看</el-button>
          <el-button type="text" @click="handledel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 上传对话框 -->
    <el-dialog v-model="open" width="800px" :title="title">
      <p></p>
      <el-form ref="emUploadRef" :model="queryParams" :rules="rules" label-width="150px">
        <el-form-item label="上传图片" prop="aiPictureId">
          <image-upload ref="imageUpload" v-model="queryParams.attach" :limit="limit" @uploadImage="uploadImage"
            @delImage="delImage" />
        </el-form-item>
        <el-form-item label="标题" prop="aiPictureTitle">
          <el-input v-model="queryParams.aiPictureTitle" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="描述" prop="aiPictureDescription">
          <el-input v-model="queryParams.aiPictureDescription" type="textarea" maxlength="300" show-word-limit
            :autosize="{ minRows: 3, maxRows: 5}" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <image-uploads ref="imagesUpload" v-model="uploadImg1" />
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleUpload">确定</el-button>
          <el-button @click="cancelUpload">取消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 商会对话框 -->
    <el-dialog v-model="showOption" width="400px" title="震情趋势" :close-on-click-modal="false">
      <p class="worrning">震情趋势已发生变化，请注意！</p>
      <el-radio v-model="radioOption" label="true" style="margin-left:10px">今日不再提醒</el-radio>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" :disabled="!radioOption" @click="ok">确定</el-button>
          <el-button @click="cancelOption">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="emphasis">
import imageUploads from './ImageUpload/index.vue'
import { listEmphasis, getEmphasis, uploadEmphasis, updateEmphasis, delEmphasis } from "@/api/assess/emphasis.js";
import { setcookieTimeFun, getCookieFun } from "@/utils/popout.js"

const { proxy } = getCurrentInstance();
const route = useRoute();
const showIcon = ref(false);
const titleOption = ref();
const loading = ref(false);
const showOption = ref(false);
const open = ref(false);
const radioOption = ref(false);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const title = ref();
const id = ref();
const krRelation = ref();
const limit = ref(1);
const krContent = ref('重点场所风险评估');
const krId = ref();
const data = reactive({
  queryParams: {
    aiPictureId: '',
    attach: [],
    aiPictureTitle: '',
    aiPictureDescription: '',
  },
  uploadImg1: [],
  dataList: [],
  rules: {
    aiPictureId: [{ required: true, message: "图片不能为空", trigger: "blur" }],
    aiPictureTitle: [{ required: true, message: "标题不能为空", trigger: "blur" }],
  }
})
const { queryParams, dataList, rules, uploadImg1 } = toRefs(data)

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 获取列表操作 */
function getList() {
  krRelation.value = route.query.id
  id.value = route.query.id;
  // id.value = '123';
  // krRelation.value = '123';
  loading.value = true;
  listEmphasis(id.value).then((response) => {
    dataList.value = response.data.lists;
    titleOption.value = response.data.ktOpinion;
    showIcon.value = Boolean(Number(response.data.ktOpinionStatus));
    loading.value = false;
  });
}
// 打开成果上传开关
function openUpload() {
  reset();
  title.value = "上传"
  open.value = true;
}

// 重置上传
function reset() {
  queryParams.value = {
    aiPictureId: undefined,
    attach: [],
    aiPictureTitle: '',
    aiPictureDescription: '',
  }
  proxy.resetForm("emUploadRef");
  uploadImg1.value = [];
}

// 成果上传
function handleUpload() {
  proxy.$refs["emUploadRef"].validate(valid => {
    if (valid) {
      if(proxy.$refs["imagesUpload"].uploadRef1!=undefined){
        proxy.$refs["imagesUpload"].uploadRef1.validate(valid1 => {
        if (valid1) {
          let query = {
              krRelation:krRelation.value,
              krContent:krContent.value,
              assInfos:[queryParams.value,...proxy.$refs["imagesUpload"].uploadImg]
            }
          if (title.value == '上传') {
            let flag = false;
            if (dataList.value.length == 0) {
              flag = false;
            } else {
              // 查看是否重复
              flag = true
            }
            flag ? updateUpload(query) : upload(query);
          } else if (title.value == '查看') {
            updateEmphasis({...query,krId:krId.value}).then(response => {
              proxy.$modal.msgSuccess("保存成功");
              open.value = false;
              getList();
            });
          }
        }
      });
      }else{
      let query = {
              krRelation:krRelation.value,
              krContent:krContent.value,
              assInfos:[queryParams.value]
            }
      if (title.value == '上传') {
            let flag = false;
            if (dataList.value.length == 0) {
              flag = false;
            } else {
              // 查看是否重复
              flag = true
            }
            flag ? updateUpload(query) : upload(query);
          } else if (title.value == '查看') {
            updateEmphasis({...query,krId:krId.value}).then(response => {
              proxy.$modal.msgSuccess("保存成功");
              open.value = false;
              getList();
            });
          }
      }
    }
  });
}
function upload(query) {
  uploadEmphasis(query).then(response => {
    proxy.$modal.msgSuccess("上传成功");
    open.value = false;
    getList();
  }).catch(() => { });
}
// 是否覆盖
function updateUpload(query) {
  proxy.$modal.confirm('是否确认覆盖重点场所风险评估内容为"' + krContent.value + '"的数据项？').then(function () {
    return uploadEmphasis(query);
  }).then(() => {
    proxy.$modal.msgSuccess("上传成功");
    open.value = false;
    getList();
  }).catch(() => { });
}

function getDetail(id) {
  getEmphasis(id).then(response => {
    if(response.data.assInfos.length>1){
      uploadImg1.value = response.data.assInfos.splice('1')
      console.log(uploadImg1.value,'ashgauygdua');
    }
    queryParams.value = response.data.assInfos[0];
    queryParams.value.attach = response.data.assInfos[0].attachmentInfoEntity?[response.data.assInfos[0].attachmentInfoEntity]:[];
    krId.value = response.data.krId;
    title.value = "查看"
    open.value = true;
  })
}

/** 查看方法*/
function handleDetil(row) {
  title.value = '查看';
  reset();
  getDetail(row.krId)
}

// 取消成果上传
function cancelUpload() {
  open.value = false;
}

/** 删除按钮操作 */
function handledel(row) {
  const id = row.krId;
  proxy.$modal.confirm('是否确认删除' + row.krContent + '的数据项？').then(function () {
    return delEmphasis(id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

// 第一个上传
function uploadImage(val) {
  queryParams.value.aiPictureId = val[0].attachId;
}

// 第一个删除
function delImage(val) {
  queryParams.value.aiPictureId = undefined;
}

// 下载
function handleDownload() {

}

// 提交
function handleSubmit() {

}

// 监听单选按钮
watch(showIcon, (val) => {
  showOption.value = val;
  if (showOption.value) {
    getTimeSetCookieFun(`emphasisCookie${route.query.id}`)
  }
})

// 获取日期时间
function getTimeSetCookieFun(name) {
  var day = getCookieFun(name) == '' ? '' : getCookieFun(name);
  if (day == '') {
    // console.log('没有cookie,说明过期或者还没有存')
    showOption.value = true;
  } else {
    var newTime = new Date().getDate();
    if (newTime > day) {
      // console.log('我是新的一天,我要弹窗')
      showOption.value = true;
    } else {
      showOption.value = false;
    }
  }
}
// 取消商会意见
function cancelOption() {
  showOption.value = false;
}
// 确认不再提示
function ok() {
  var newTime = new Date().getDate();
  setcookieTimeFun(`emphasisCookie${route.query.id}`, newTime, 1)
  showOption.value = false;
}

getList();
</script>
<style lang="scss" scoped>
.hide {
  display: none !important;
}

.container {
  margin: 0px auto;
  height: 100%;
  width: 1200px;
  overflow-x: hidden;
  overflow-y: auto;

  .worrning {
    width: 340px;
    height: 110px;
    color: #3d4a5d;
    font-size: 18px;
    line-height: 26px;
    background: #f5f9fe;
    margin: 30px auto 10px;
    padding: 15px;
  }

  .header {
    padding: 10px 0px;

    .title {
      font-size: 18px;
      font-weight: bold;
    }

    div {
      height: 60px;
      line-height: 60px;
      margin-top: 10px;

      &.icon {
        padding: 0 20px;
        background: #f5f9fe;
      }
    }
  }
}
</style>
  